package com.ruoyi.service.impl;

import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.service.IGrDetailService;
import com.ruoyi.service.IGrDetailService;
import com.ruoyi.web.domain.GrDetail;
import com.ruoyi.web.domain.GrVtDetailUpdateHistory;
import com.ruoyi.web.mapper.GrDetailMapper;
import com.ruoyi.web.mapper.GrDetailUpdateHistoryMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Collections;
import java.util.Date;
import java.util.List;
import java.util.Objects;

import static com.ruoyi.common.utils.ShiroUtils.getLoginName;
import static com.ruoyi.common.utils.ShiroUtils.getUserId;

@Service
public class GrDetailServiceImpl implements IGrDetailService {
    @Resource
    private GrDetailMapper crDetailMapper;
    @Resource
    private GrDetailUpdateHistoryMapper grDetailUpdateHistoryMapper;

    @Override
    public List<GrDetail> selectGrDetailList(GrDetail crDetail) {
        return crDetailMapper.selectGrDetailList(crDetail);
    }

    @Override
    public List<GrDetail> selectByCategoryId(Integer categoryId) {
        return crDetailMapper.selectByCategoryId(categoryId);
    }

    @Override
    @Transactional
    public Integer updateGrDetail(GrDetail crDetail) {
        GrDetail exist = crDetailMapper.selectGrDetailById(crDetail.getId());
        if(Objects.isNull(exist)){
            throw new RuntimeException("品类不存在 无法更新");
        }
        exist.setVtName(crDetail.getVtName());
        exist.setVtPrice(crDetail.getVtPrice());
        crDetailMapper.updateGrDetail(exist);
        Long now = System.currentTimeMillis();
        GrVtDetailUpdateHistory history = new GrVtDetailUpdateHistory();
        history.setSourceId(exist.getId());
        history.setVtName(exist.getVtName());
        history.setVtType(1);
        history.setVcrId(exist.getVcrId());
        history.setVtPrice(exist.getVtPrice());
        history.setThisType(1);
        history.setUnit("元/斤");
        history.setCreateBy(getUserId());
        history.setCreateTime(now);
        history.setUpdateBy(getUserId());
        history.setUpdateTime(now);
        grDetailUpdateHistoryMapper.insert(history);

        return exist.getVcrId();
    }

    @Override
    public GrDetail selectGrDetailById(Integer crDetailId) {
        return crDetailMapper.selectGrDetailById(crDetailId);
    }

    @Override
    public Integer deleteGrDetailById(Integer cardId) {
        return crDetailMapper.deleteGrDetailById(cardId);
    }

    @Override
    @Transactional
    public Integer saveGrDetail(GrDetail role) {
        if(StringUtils.isEmpty(role.getVtName())){
            throw new RuntimeException("品类名称不能为空");
        }
        Long now = System.currentTimeMillis();
        crDetailMapper.saveGrDetail(role);
        GrVtDetailUpdateHistory history = new GrVtDetailUpdateHistory();
        history.setSourceId(role.getId());
        history.setVtName(role.getVtName());
        history.setVtType(1);
        history.setVcrId(role.getVcrId());
        history.setVtPrice(role.getVtPrice());
        history.setThisType(1);
        history.setUnit("元/斤");
        history.setCreateBy(getUserId());
        history.setCreateTime(now);
        history.setUpdateBy(getUserId());
        history.setUpdateTime(now);
        grDetailUpdateHistoryMapper.insert(history);
        return 1;
    }
}
